﻿using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyWordAddIn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Document doc = null;
        private Microsoft.Office.Interop.Word.Application app = null; //Office2013 15.0.0.0
        private Message msg = null;


        private void Form1_Load(object sender, EventArgs e)
        {
            app = Globals.ThisAddIn.Application;
            doc = Globals.ThisAddIn.Application.ActiveDocument;
            msg = new Message(tbMsg);
        }

        #region Browser
        /// <summary>
        /// 单独获取Browser
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBro_Click(object sender, EventArgs e)
        {
            WordBrowser bro = new WordBrowser(app);
            bro.GetBrowser(msg);

        }

        /// <summary>
        /// 循环获取Browser
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBroLoop_Click(object sender, EventArgs e)
        {
            WordBrowser bro = new WordBrowser(app);
            bro.LoopBrowser(msg);
        }
        #endregion

        #region Paragraphs
        /// <summary>
        /// 循环Paragraph
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPara_Click(object sender, EventArgs e)
        {
            WordParagraph pa = new WordParagraph(app);
            pa.LoopParagraph(msg);
        }

        /// <summary>
        /// 添加一个新段落
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnParaAdd_Click(object sender, EventArgs e)
        {
            WordParagraph pa = new WordParagraph(app);
            pa.AddParagraph();
        }

        /// <summary>
        /// 选择段落的上一段落
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnParaPre_Click(object sender, EventArgs e)
        {
            WordParagraph pa = new WordParagraph(app);
            pa.PrePara(msg);
        }

        /// <summary>
        /// 选择段落的下一段落
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnParaNext_Click(object sender, EventArgs e)
        {
            WordParagraph pa = new WordParagraph(app);
            pa.NextPara(msg);
        }
        #endregion

        #region ContentControl

        /// <summary>
        /// 添加一个ContentControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCtlAdd_Click(object sender, EventArgs e)
        {
            WordContentControl ctl = new WordContentControl(doc);
            ctl.AddContentControl(msg, "Test");
        }

        /// <summary>
        /// 根据标题获取ContentControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCtlGet_Click(object sender, EventArgs e)
        {
            WordContentControl ctl = new WordContentControl(doc);
            ctl.GetContentControl(msg, "Test");
        }

        /// <summary>
        /// 根据名称删除ContentControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCtlDel_Click(object sender, EventArgs e)
        {
            WordContentControl ctl = new WordContentControl(doc);
            ctl.DelContentControl(msg, "Test");
        }

        #endregion

        #region Range
        /// <summary>
        /// 获取所有范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngAll_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.GetAllRange(msg);
        }

        /// <summary>
        /// 获取范围内所有链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngHyper_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.GetHyperRange(msg);
        }

        /// <summary>
        /// 在范围End处添加内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngInsertAfter_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.InsertAfter(msg," 你在干什么!");
        }

        /// <summary>
        /// 把当前Range的内容全部删除，之后再插入一个段落，这就相当于有两个段落
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngInsertPara_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.InsertParagraph(msg);
        }

        /// <summary>
        /// 在范围的End处添加新段落，会沿用上一段落样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngInsertParaAfter_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.InsertParagraphAfter(msg);
        }

        /// <summary>
        /// 在范围的Start处添加新段落，会沿用上一段落样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngInsertParaBefore_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.InsertParagraphBefore(msg);
        }

        /// <summary>
        /// 删除范围所有内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRngDel_Click(object sender, EventArgs e)
        {
            WordRange rng = new WordRange(doc);
            rng.DelParagraph(msg);
        }
        #endregion

        #region Style
        /// <summary>
        /// 获取所有样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetStyle_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.GetAllStyle(msg);
        }

        /// <summary>
        /// 设置一个现有的样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetStyle_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.SetExsitsStyle(msg);
        }

        /// <summary>
        /// 设置项目符号样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetSymbolStyle_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.SetSymbolStyle(msg);
        }

        /// <summary>
        /// 设置数字样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetNumberStyle_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.SetNumberStyle(msg);
        }

        /// <summary>
        /// 设置自定义样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetCustomStyle_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.SetCustomStyle(msg);
        }

        /// <summary>
        /// 获取ListValue的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetListValue_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.GetListValue(msg);
        }
        /// <summary>
        /// 设置缩进
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetIndent_Click(object sender, EventArgs e)
        {
            WordStyle style = new WordStyle(doc);
            style.SetIndent(msg);
        }
        #endregion

        #region XMLParts
        private void btnGetParts_Click(object sender, EventArgs e)
        {
            WordXMLParts parts= new WordXMLParts(doc);
            parts.GetXMLParts(msg,"Test");
        }

        private void btnSetParts_Click(object sender, EventArgs e)
        {
            WordXMLParts parts = new WordXMLParts(doc);
            parts.SetXMLParts(msg, "Test");
        }

        private void btnDelParts_Click(object sender, EventArgs e)
        {
            WordXMLParts parts = new WordXMLParts(doc);
            parts.DelXMLParts("Test");
        }


        #endregion

        #region application
        WordApplication wordApp = null;
        /// <summary>
        /// 新建一个application 对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAppNew_Click(object sender, EventArgs e)
        {
            if (wordApp == null)
            {
                wordApp = new WordApplication();
            }
            wordApp.NewApp(msg);
        }
        /// <summary>
        /// 结束当前application对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAppQuit_Click(object sender, EventArgs e)
        {
            if (wordApp != null)
            {
                wordApp.QuitApp(msg);
            }
        }
        #endregion

       
    }
}
